Imports System.Configuration
Imports System.Web.Configuration
Imports System.Web

Public Class EventService
    Private Shared _provider As EventProvider = Nothing
    Private Shared _providers As EventCollection = Nothing
    Private Shared _lock As Object = New Object
    Private Shared isInitialized As Boolean = False
    Private Shared initializationException As Exception

    Public ReadOnly Property Provider() As EventProvider
        Get
            Return _provider
        End Get
    End Property

    Public ReadOnly Property Providers() As EventCollection
        Get
            Return _providers
        End Get
    End Property

    Private Shared Sub LoadProviders()
        Try
            'Get the feature's configuration info
            Dim qc As EventConfiguration = CType(ConfigurationManager.GetSection("EventProvider"), EventConfiguration)
            If ((qc.DefaultProvider Is Nothing) _
                        OrElse ((qc.Providers Is Nothing) _
                        OrElse (qc.Providers.Count < 1))) Then
                Throw New System.Configuration.Provider.ProviderException(("You must specify a valid default provider."))
            End If
            _providers = New EventCollection
            ProvidersHelper.InstantiateProviders(qc.Providers, _providers, GetType(EventProvider))
            _providers.SetReadOnly()
            _provider = _providers(qc.DefaultProvider)
            If (_provider Is Nothing) Then
                Throw New ConfigurationErrorsException("You must specify a default provider for the feature.", qc.ElementInformation.Properties("defaultProvider").Source, qc.ElementInformation.Properties("defaultProvider").LineNumber)
            End If
        Catch ex As Exception
            initializationException = ex
            isInitialized = True
            Throw ex
        End Try
        isInitialized = True
        'error-free initialization
    End Sub
    Public Shared Function GetEvents() As Generic.List(Of DataTypes.EventItem)
        LoadProviders()
        Return _provider.GetEvents()
    End Function
    Public Shared Function GetEvents(ByVal RangeStart As Date, ByVal RangeEnd As Date) As Generic.List(Of DataTypes.EventItem)
        LoadProviders()
        Return _provider.GetEvents(RangeStart, RangeEnd)
    End Function
    Public Shared Function GetEvent(ByVal EventId As String) As DataTypes.EventItem
        LoadProviders()
        Return _provider.GetEvent(EventId)
    End Function
    Public Shared Function GetSchedule(ByVal EventId As String) As Generic.List(Of DataTypes.EventScheduleItem)
        LoadProviders()
        Return _provider.GetSchedule(EventId)
    End Function
    Public Shared Sub AddEvent(ByVal EventItem As DataTypes.EventItem, ByVal Username As String)
        LoadProviders()
        _provider.AddEvent(EventItem, Username)
    End Sub
    Public Shared Sub AddScheduleItem(ByVal EventId As String, ByVal EventScheduleItem As DataTypes.EventScheduleItem, ByVal Username As String)
        LoadProviders()
        _provider.AddScheduleItem(EventId, EventScheduleItem, Username)
    End Sub
    Public Shared Sub UpdateEvent(ByVal EventItem As DataTypes.EventItem, ByVal Username As String)
        LoadProviders()
        _provider.UpdateEvent(EventItem, Username)
    End Sub
    Public Shared Sub UpdateScheduleItem(ByVal EventId As String, ByVal EventScheduleItem As DataTypes.EventScheduleItem, ByVal Username As String)
        LoadProviders()
        _provider.UpdateScheduleItem(EventId, EventScheduleItem, Username)
    End Sub
    Public Shared Sub RemoveEvent(ByVal EventId As String, ByVal Username As String)
        LoadProviders()
        _provider.RemoveEvent(EventId, Username)
    End Sub
    Public Shared Sub RemoveScheduleItem(ByVal EventScheduleId As String, ByVal Username As String)
        LoadProviders()
        _provider.RemoveScheduleItem(EventScheduleId, Username)
    End Sub
End Class
